﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace Project
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
         
            BoinDataGridView();
        }
        public void BoinDataGridView()
        {
            
            this.dataGridView1.AutoGenerateColumns = false;
            string sql = "select * from Room where 1=1";
            string name = this.textBox1.Text.ToString();
            if (!string.IsNullOrEmpty(name))
            {
                sql += string.Format(" and  RoomName like '%{0}%'",name);
            }
            this.dataGridView1.DataSource = FrmHotel.DBHelper.SelectForTable(sql);
            
          

            this.dataGridView6.AutoGenerateColumns = false;
            string sql1 = "select r.RmId,r.RmName,m.RoomName,s.StName from RoomInfo r,RmState s,Room m where m.RoomId=r.RmRoom and s.StId=r.RmState  ";
            this.dataGridView6.DataSource = FrmHotel.DBHelper.SelectForTable(sql1);

            this.dataGridView2.AutoGenerateColumns = false;
            string sql2 = "select e.*,s.ESName,b.BranchName from EyInfo e,EyState s,Branch b where b.BranchId=e.EyBranchId and s.ESId=e.EyState ";
            this.dataGridView2.DataSource = FrmHotel.DBHelper.SelectForTable(sql2);

            this.dataGridView3.AutoGenerateColumns = false;
            string sql3 = "select c.*,k.CKCin,y.YzSatseName from  CetaeInfo c,checkInfo k,YzSatse y where c.CeId=k.Ckname and y.YzSatseId=k.CKState";
            this.dataGridView3.DataSource = FrmHotel.DBHelper.SelectForTable(sql3);

            this.dataGridView4.AutoGenerateColumns = false;
            string sql4 = "select s.*,c.CetaeName from  SuiKeInfo s,CetaeClass c where s.SktaeClass=c.CetaeId";
            this.dataGridView4.DataSource = FrmHotel.DBHelper.SelectForTable(sql4);

            this.dataGridView5.AutoGenerateColumns = false;
            string sql5 = "select r.*,y.YzSatseName from  ResvInfo r,YzSatse y where r.RvSatse=y.YzSatseId";
            this.dataGridView5.DataSource = FrmHotel.DBHelper.SelectForTable(sql5);
        }

        private void button8_Click_1(object sender, EventArgs e)
        {
            //判断是否选中一行
            if (this.dataGridView1.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请至少选择一条数据");
                return;
            }
            DialogResult dr = MessageBox.Show("真的要删除吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                //获取选中的数据
                object o = this.dataGridView1.SelectedRows[0].Cells[0].Value;
                int id = Convert.ToInt32(o);

                string sql1 = string.Format("select count(1) from RoomInfo r,Room m where m.RoomId=RmRoom and m.RoomId={0}",id);
                int result1 = Convert.ToInt32(FrmHotel.DBHelper.SelectOne(sql1));
                if (result1>0)
                {
                    MessageBox.Show("本类型下有房间,不可以删除！");
                    return;
                }
                else
                {
                   
                    string sql = "delete from Room where RoomId= " + id;
                    int result = FrmHotel.DBHelper.ExecuteUpdate(sql);

                    if (result>0)
                    {
                        MessageBox.Show("删除成功");
                        BoinDataGridView();
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                }
               
            }
        }

        private void button7_Click_1(object sender, EventArgs e)
        {
            //判断是否选中一行
            if (this.dataGridView1.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请至少选择一条数据");
                return;
            }

                object o = this.dataGridView1.SelectedRows[0].Cells[0].Value;
                int id = Convert.ToInt32(o);
                FXxiu fxx = new FXxiu();
                fxx.id = id;
                fxx.ShowDialog();
                BoinDataGridView();
        }

        private void tabPage5_Click(object sender, EventArgs e)
        {
            
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void tabControl2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void tabPage3_Click_1(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick_2(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void panel1_Paint_1(object sender, PaintEventArgs e)
        {

        }

     
        private void tabControl3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click_1(object sender, EventArgs e)
        {

        }
        private void tabPage6_Click(object sender, EventArgs e)
        {

        }

        private void tabPage7_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click_3(object sender, EventArgs e)
        {
            
            
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            BoinDataGridView();
        }

        private void label1_Click(object sender, EventArgs e)
        {
           
        }
        private void button19_Click(object sender, EventArgs e)
        {
            FJtian fjt = new FJtian();
            fjt.ShowDialog();
            BoinDataGridView();

        }

        private void button20_Click(object sender, EventArgs e)
        {
            //判断是否选中一行
            if (this.dataGridView1.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请至少选择一条数据");
                return;
            }
            DialogResult dr = MessageBox.Show("真的要删除吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                //获取选中的数据
                object o = this.dataGridView6.SelectedRows[0].Cells[0].Value;
                int id = Convert.ToInt32(o);

                string sql1 = "select RmState from RoomInfo where RmId="+id;
                SqlDataReader reader = FrmHotel.DBHelper.SelectMore(sql1);
                int oo = -1;
                if (reader.Read())
	                    {
                            oo = Convert.ToInt32(reader["RmState"]);
	                    }
                reader.Close();
               
                if (oo==1 || oo==4)
                {
                    string sql = "delete from RoomInfo where RmId= " + id;
                    int result = FrmHotel.DBHelper.ExecuteUpdate(sql);

                    if (result > 0)
                    {
                        MessageBox.Show("删除成功");
                        BoinDataGridView();
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                        return;
                    }
                   
                }
                else if (oo==2)
                {

                    MessageBox.Show("本房间有宾客预住,不可以删除！");
                    return;
                }
                else
                {
                    MessageBox.Show("本房间有宾客再住,不可以删除！");
                    return;
                }
                
            }
        }

        private void button6_Click_2(object sender, EventArgs e)
        {
            YGtian ygt = new YGtian();
            ygt.ShowDialog();
            BoinDataGridView();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            
            if(this.dataGridView3.SelectedRows.Count <=0)
            {
                MessageBox.Show("请至少选择一条数据");
            }
            DialogResult dia =MessageBox.Show("真的要删除吗？","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if(dia==DialogResult.Yes)
                {
                    object obj = this.dataGridView3.SelectedRows[0].Cells[0].Value;
                    int bs = Convert.ToInt32(obj);
                    string sql="select YzSatseId from YzSatse where YzSatseId="+bs;
                    SqlDataReader reader1 = FrmHotel.DBHelper.SelectMore(sql);
                    int ooo=-1;
                    if(reader1.Read())
                    {
                        ooo =Convert.ToInt32(reader1["YzSatseId"]);
                    }
                    reader1.Close();
                    if (ooo == 3)
                    {
                        string sqlsq = "delete * from CetaeInfo where CeId= " + bs;
                        int re = FrmHotel.DBHelper.ExecuteUpdate(sqlsq);
                        if (re > 0)
                        {
                            MessageBox.Show("删除成功");
                            BoinDataGridView();
                        }
                        else
                        {
                            MessageBox.Show("删除失败");
                        }
                    }
                    else if (ooo == 1)
                    {
                        MessageBox.Show("该宾客再住期间不能删除");
                        return;
                    }
                    else
                    {
                        MessageBox.Show("该宾客预住期间不能删除");
                        return;
                    }
                }
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            if (this.dataGridView2.SelectedRows.Count <= 0)
            {
                MessageBox.Show("至少选择一条数据");
            }
            DialogResult dr1 = MessageBox.Show("真的要删除吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if(dr1==DialogResult.Yes)
            {
                object o1 = this.dataGridView2.SelectedRows[0].Cells[0].Value;
                 int id1 = Convert.ToInt32(o1);
                 string sql8 = string.Format("select EyState from EyInfo where EyId={0}", id1);
                 SqlDataReader reader = FrmHotel.DBHelper.SelectMore(sql8);
                 int o3 = 0;
                if(reader.Read())
                {
                    o3 = Convert.ToInt32(reader["EyState"]);
                }
                reader.Close();
                if (o3 == 2)
                {
                    string sql2 = string.Format("delete  EyInfo where EyId={0} and EyState=2 ", id1);
                    int result = FrmHotel.DBHelper.ExecuteUpdate(sql2);
                    if (result > 0)
                    {
                        MessageBox.Show("删除成功");
                        BoinDataGridView();
                    }
                    else
                    {
                         MessageBox.Show("删除失败");
                    return;
                    } 
                }
                else 
                {
                      MessageBox.Show("只有离职员工方可删除！");
                        return;
                }   
            }

        }

        private void button4_Click_2(object sender, EventArgs e)
        {
            if(this.dataGridView2.SelectedRows.Count<=0)
            {
                MessageBox.Show("至少选择一条数据");
                return;
            }
            object o4 = this.dataGridView2.SelectedRows[0].Cells[0].Value;
            int Idi = Convert.ToInt32(o4);

            YGxiu ygx = new YGxiu();
            ygx.id = Idi;
            ygx.ShowDialog();
          
            BoinDataGridView();
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
           
            this.dataGridView6.AutoGenerateColumns = false;
            string s = "select * from RoomInfo r,RmState m ,Room o where r.RmRoom=o.RoomId and r.RmState=m.StId  and 1=1";
            string xm = this.textBox2.Text;
            if(!string.IsNullOrEmpty(xm))
            {
                s += string.Format(" and RmName like '%{0}%'",xm);
            }
            this.dataGridView6.DataSource = FrmHotel.DBHelper.SelectForTable(s);

        }

        private void button21_Click(object sender, EventArgs e)
        {
            if(this.dataGridView6.SelectedRows.Count<=0)
            {
             MessageBox.Show("请至少选择一条数据");
                return;
            }
            object o3 =this.dataGridView6.SelectedRows[0].Cells[0].Value;
            int fid = Convert.ToInt32(o3);
             FJxiu fjx =new FJxiu ();
             fjx.id = fid;
             fjx.ShowDialog();
            BoinDataGridView();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            this.dataGridView3.AutoGenerateColumns = false;
            string sqll = "select c.*,h.CKCin,y.YzSatseName  from CetaeInfo c ,checkInfo h,YzSatse y where h.CKState=y.YzSatseId";
            string xing = this.textBox4.Text;
            if (!string.IsNullOrEmpty(xing))
            {
                sqll += string.Format(" and CeName like '%{0}%'",xing);
            }
            this.dataGridView3.DataSource = FrmHotel.DBHelper.SelectForTable(sqll);
        }

        private void button9_Click_2(object sender, EventArgs e)
        {
            if(this.dataGridView3.SelectedRows.Count <=0)
            {
                MessageBox.Show("请至少选择一条数据");
                return;
            }
            object Obj = this.dataGridView3.SelectedRows[0].Cells[0].Value;
            int bid = Convert.ToInt32(Obj);

            BKxiu bkx = new BKxiu();
            bkx.id = bid;
            bkx.ShowDialog();
            BoinDataGridView();
        }

        private void textBox3_TextChanged_1(object sender, EventArgs e)
        {
            this.dataGridView2.AutoGenerateColumns = false;
            string sl = "select *  from EyInfo e ,Branch b, EyState s where e.EyBranchId=b.BranchId and e.EyState=s.ESId and 1=1";
            string yxm = this.textBox3.Text;
            if(!string.IsNullOrEmpty(yxm))
            {
                sl += string.Format(" and EyName like '%{0}%'",yxm);

            }
            this.dataGridView2.DataSource = FrmHotel.DBHelper.SelectForTable(sl);
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            this.dataGridView5.AutoGenerateColumns = false;
            string sqlsql = "select r.*,y.YzSatseName from ResvInfo r,YzSatse y where r.RvSatse=YzSatseId  and 1=1 ";
            string dxm = this.textBox6.Text;
            if(!string.IsNullOrEmpty(dxm))
            {
                sqlsql += string.Format("  and RvName like '%{0}%'",dxm);
            }
            this.dataGridView5.DataSource = FrmHotel.DBHelper.SelectForTable(sqlsql);

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            this.dataGridView4.AutoGenerateColumns = false;
            string sqlyj = "select s.*,c.CetaeName from SuiKeInfo s,CetaeClass c where SktaeClass =CetaeId and  1=1";
            string skxm = this.textBox5.Text;
            if(!string.IsNullOrEmpty(skxm))
            {
                sqlyj += string.Format(" and SkName like '%{0}%'",skxm);
            }
            this.dataGridView4.DataSource = FrmHotel.DBHelper.SelectForTable(sqlyj);
        }

        private void button1_Click_3(object sender, EventArgs e)
        {
            if (this.dataGridView4.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请至少选择一条数据");
                return;
            }
            object Obj1 = this.dataGridView4.SelectedRows[0].Cells[0].Value;
            int bid1 = Convert.ToInt32(Obj1);
            YKxiu skx = new YKxiu();
            skx.id = bid1;
            skx.ShowDialog();
            BoinDataGridView();
        }

        private void button16_Click(object sender, EventArgs e)
        {
            if(this.dataGridView5.SelectedRows.Count <=0)
            {
                MessageBox.Show("请至少选择一条数据");
                return;
            }
            object OBJ = this.dataGridView5.SelectedRows[0].Cells[0].Value;
            int ykid = Convert.ToInt32(OBJ);
            YKxiu ykx = new YKxiu();
            ykx.id = ykid;
            ykx.ShowDialog();
            BoinDataGridView();
        }

        private void button2_Click_3(object sender, EventArgs e)
        {
             
            if(this.dataGridView4.SelectedRows.Count<=0)
            {
                MessageBox.Show("请至少选中一条数据");
                return;
            }
            DialogResult d = MessageBox.Show("真的要删除吗？", "提示信息",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if(d==DialogResult.Yes)
            {
                object j = this.dataGridView4.SelectedRows[0].Cells[0].Value;
                int sjh = Convert.ToInt32(j);
                string sql1 = string.Format("select SkFangHao from SuiKeInfo where SkeId={0}",sjh);
                string sqls = "delete from SuiKeInfo where SkeId = "+sjh;
                int resu = FrmHotel.DBHelper.ExecuteUpdate(sqls);
                if (resu > 0)
                {
                    MessageBox.Show("删除成功");
                    BoinDataGridView();
                }
                else 
                {
                    MessageBox.Show("删除失败");
                }
            }
            
        }

        private void button17_Click(object sender, EventArgs e)
        {
            if (this.dataGridView5.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请至少选中一条数据");
                return;
            }
            DialogResult d = MessageBox.Show("真的要删除吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
            {
                object j1 = this.dataGridView5.SelectedRows[0].Cells[0].Value;
                int sjh1 = Convert.ToInt32(j1);
                string sqlsql2 = "select * from ResvInfo  where RvId=" + sjh1;
                SqlDataReader reader = FrmHotel.DBHelper.SelectMore(sqlsql2);
                int ii = 0;
                if (reader.Read())
                {
                    ii = Convert.ToInt32(reader["RvSatse"]);
                }
                reader.Close();
                if (ii==3)
                {
                    string sqls = "delete from RoomInfo where RmId = " + sjh1;
                    int resu = FrmHotel.DBHelper.ExecuteUpdate(sqls);
                    if (resu > 0)
                    {
                        MessageBox.Show("删除成功");
                        BoinDataGridView();
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                        return;
                    }
                }
                else if (ii==1)
                {
                    MessageBox.Show("预客入住期间，不能删除！");
                    return;
                }
                else
                {
                    MessageBox.Show("预客预住期间，不能删除！");
                    return;
                }
               
            }
            
        }
        }
    }

